package controllers
{
	import models.RESTResponse;
	import models.UMLAttribute;
	
	import mx.collections.ArrayCollection;
		
	public class UMLAttributeManager
	{
		private var _current:UMLAttribute;
		
		public function UMLAttributeManager() {
			_current 	= null;
		}
		/*
		public function updateList(project:Project, callback:Function) : void {
			HTTPController.GET('/classes/project/' + project.oid, null, function (response:RESTResponse) : void {
				upadateListHandler(response);
				callback(response);
			});
		}
		
		public function upadateListHandler(response:RESTResponse) : void {
			if(response.success()) {
				// ATTENTION ! ne pas faire de new ... ça casse le data binding ! 
				_classes.removeAll();
				for each(var xml:XML in response.data()) {
					var c:UMLClass = new UMLClass();
					c.setXML(xml);
					_classes.addItem(c);
				}
			} else {
				UIErrorController.handleRESTErrorCode(response.errorCode());
			}
		}
			import models.UMLAttribute;
			
		*/
		public function createAttribute(umlAttribute:UMLAttribute) : void {
			HTTPController.POST('/classes/class/attributes', umlAttribute.toXML(), function (response:RESTResponse) : void {
				if(response.success()) {
					
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});
		}
		
		public function updateAttribute(umlAttribute:UMLAttribute) : void {
			HTTPController.PUT('/classes/class/' + umlAttribute.classId + '/attributes/attribute/' + umlAttribute.oid, umlAttribute.toXML(), function (response:RESTResponse) : void {
				if(response.success()) {
					
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});	
		}
		
		public function saveAttribute(classId:int, name:String) : void {
			if(_current == null) {
				var umlAttribute:UMLAttribute = new UMLAttribute(classId, name);
				createAttribute(umlAttribute);
			} else {
				_current.classId = classId;
				_current.name = name;
				
				updateAttribute(_current);
				_current = null;
			}
		}
		
		public function set currentAttribute(umlAttribute:UMLAttribute) : void {
			_current = umlAttribute;
		} 
		
		public function get currentAttribute() : UMLAttribute {
			return _current;
		}
	}
}